package com.pxample.config;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * 既支持直连又支持服务发现的调用
 *
 */
@Component
public class GenericRest {

    @Autowired
    private RestTemplate lbRestTemplate;

    @Autowired
    private RestTemplate formRestTemplate;

    @Autowired
    private RestTemplate noneCoreRestTemplate;

    @Autowired
    private RestTemplate kycRestTemplate;

    public <T> ResponseEntity<T> post(String url, Object reqBody, ParameterizedTypeReference<T> responseType) {
        HttpHeaders headers = new HttpHeaders();
        headers.set("x-terminalType", "app");
        HttpEntity entity = new HttpEntity(reqBody, headers);
        return lbRestTemplate.exchange(url, HttpMethod.POST, entity, responseType);
    }

    public <T> ResponseEntity<T> postForm(String url, Object reqBody, ParameterizedTypeReference<T> responseType) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.set("x-terminalType", "app");
        HttpEntity entity = new HttpEntity(reqBody, headers);
        return formRestTemplate.exchange(url, HttpMethod.POST, entity, responseType);
    }
    public <T> ResponseEntity<T> postFormByIp(String url, Object reqBody, ParameterizedTypeReference<T> responseType,String userIp) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.set("x-terminalType", "app");
        headers.set("x-gtja-ip",userIp);
        HttpEntity entity = new HttpEntity(reqBody, headers);
        return formRestTemplate.exchange(url, HttpMethod.POST, entity, responseType);
    }

    //大数据KYC使用
    public <T> ResponseEntity<T> postFormKyc(String url, Object reqBody, ParameterizedTypeReference<T> responseType) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.set("x-terminalType", "app");
        HttpEntity entity = new HttpEntity(reqBody, headers);
        return kycRestTemplate.exchange(url, HttpMethod.POST, entity, responseType);
    }

    // POST-透明转发，携带header。 一期是Matrix在使用。
    public <T> ResponseEntity<T> postFormTransparent(HttpServletRequest request, String url, Object reqBody, ParameterizedTypeReference<T> responseType) {

        // 新建headers，并把原请求头数据装填进去
        HttpHeaders headers = new HttpHeaders();
        if(request!=null){
            Enumeration<String> headerNames = request.getHeaderNames();
            while(headerNames.hasMoreElements()){
                String headerName = headerNames.nextElement();
                headers.set(headerName, request.getHeader(headerName));
            }
        }
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.set("x-terminalType", "app");
        HttpEntity entity = new HttpEntity(reqBody, headers);
        return noneCoreRestTemplate.exchange(url, HttpMethod.POST, entity, responseType);
    }

    public <T> ResponseEntity<T> get(String url, ParameterizedTypeReference<T> responseType) {
        return lbRestTemplate.exchange(url, HttpMethod.GET, HttpEntity.EMPTY, responseType);
    }

}
